package com.lwq.http.threads.main;

import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.log4j.Logger;

import com.lwq.http.threads.users.UserThread;
import com.lwq.http.threads.utils.TimeThread;
import com.lwq.http.utils.model.HttpParamsMap;
import com.lwq.http.utils.model.HttpResult;

/**
* @author 作者 linweiqiang
* @version 创建时间：2017年10月19日 上午11:50:09
* 类说明
*/
public class MainThread {
	private Logger logger = Logger.getLogger(MainThread.class);
	private  PoolingHttpClientConnectionManager phccm = null;
	private  CloseableHttpClient hc = null;
	private  String uri = null;
	private  Map<String,String> params = null;
	private  int uNumber = 1;
	private  int uThreads = 80;
	private  long uSleepT =  0;
	private  long sleepT =  0;
	private  long uTimeOut =  1000*60;
	private  long timeOut =  1000*60;
	private  int threads = 80;
	private  int number = 1;
	private  long startTime = 1000;
	private  long sleepTime = 1000;
	private  int maxTotal = 2;
	private  int maxPerRoute = 1;
	private  HttpParamsMap hpm = null;
	private HttpResult hr = null;
	

	public MainThread() {
	}
	public MainThread(String uri ,HttpParamsMap hpm ,HttpResult hr) {
		this.uri = uri;
		this.hpm = hpm;
		this.hr = hr;
	}
	public MainThread(String uri , Map<String,String> params ,HttpResult hr) {
		this.uri = uri;
		this.params = params;
		this.hr = hr;
	}
	public MainThread(String uri , Map<String,String> params ,HttpParamsMap hpm ,HttpResult hr) {
		this.uri = uri;
		this.params = params;
		this.hpm = hpm;
		this.hr = hr;
	}
	
	/**
	 * 1、MaxtTotal是整个池子的大小；
	 * 2、DefaultMaxPerRoute是根据连接到的主机对MaxTotal的一个细分
	 * MaxtTotal=400 DefaultMaxPerRoute=200 到每个主机的并发最多只有200；即加起来是400（但不能超过400）；
	 */
	public void threadStart(){
		phccm = new PoolingHttpClientConnectionManager();
		phccm.setMaxTotal(maxTotal);
		phccm.setDefaultMaxPerRoute(maxPerRoute);
		hc = HttpClients.custom().setConnectionManager(phccm).build();
		Thread thread = new Thread(new TimeThread(hc, uri, params, hpm, hr, startTime, sleepTime, uNumber, uThreads, uSleepT, uTimeOut, number, threads, sleepT, timeOut));
		logger.info("start");
		thread.start();
	}
	/**
	 * 请求的uri
	 * @param uri
	 */
	public void setUri(String uri) {
		this.uri = uri;
	}
	/**
	 * 请求的固定参数Map<String,String>
	 * @param params
	 */
	public void setParams(Map<String, String> params) {
		this.params = params;
	}
	/**
	 * 每个用户请求的次数
	 * @param uNumber
	 */
	public void setuNumber(int uNumber) {
		this.uNumber = uNumber;
	}
	/**
	 * 用户每次请求的线程数（默认为80）
	 * @param uThreads
	 */
	public void setuThreads(int uThreads) {
		this.uThreads = uThreads;
	}
	/**
	 * 当个用户请求超时
	 * @param uTimeOut
	 */
	public void setuTimeOut(long uTimeOut) {
		this.uTimeOut = uTimeOut;
	}
	/**
	 * 所有用户请求超时
	 * @param timeOut
	 */
	public void setTimeOut(long timeOut) {
		this.timeOut = timeOut;
	}
	/**
	 * 用户的线程数
	 * @param threads
	 */
	public void setThreads(int threads) {
		this.threads = threads;
	}
	/**
	 * 用户的个数
	 * @param number
	 */
	public void setNumber(int number) {
		this.number = number;
	}
	/**
	 * 1、MaxtTotal是整个池子的大小；
	 * 2、DefaultMaxPerRoute是根据连接到的主机对MaxTotal的一个细分
	 * MaxtTotal=400 DefaultMaxPerRoute=200 到每个主机的并发最多只有200；即加起来是400（但不能超过400）；
	 * @param maxTotal
	 */
	public void setMaxTotal(int maxTotal) {
		this.maxTotal = maxTotal;
	}
	/**
	 * 1、MaxtTotal是整个池子的大小；
	 * 2、DefaultMaxPerRoute是根据连接到的主机对MaxTotal的一个细分
	 * MaxtTotal=400 DefaultMaxPerRoute=200 到每个主机的并发最多只有200；即加起来是400（但不能超过400）；
	 * @param maxPerRoute
	 */
	public void setMaxPerRoute(int maxPerRoute) {
		this.maxPerRoute = maxPerRoute;
	}
	/**
	 * 每个用户发起请求间隔的时间
	 * @param uSleepT
	 */
	public void setuSleepT(long uSleepT) {
		this.uSleepT = uSleepT;
	}
	/**
	 * 每隔时间段内有一个用户开始请求
	 * @param sleepT
	 */
	public void setSleepT(long sleepT) {
		this.sleepT = sleepT;
	}
	
	/**
	 * 返回结果处理方法
	 * @param hr
	 */
	public void setHr(HttpResult hr) {
		this.hr = hr;
	}
	/**
	 * 线程要跑多久
	 * @param startTime
	 */
	public void setStartTime(long startTime) {
		this.startTime = startTime;
	}
	/**
	 * 线程多久执行一次
	 * @param sleepTime
	 */
	public void setSleepTime(long sleepTime) {
		this.sleepTime = sleepTime;
	}
	
}
